package com.edu.yunnote.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTime {
	/**
	 * 获取电脑当前时间
	 * @return
	 */
	public static Timestamp getDateTime() {
		Calendar calendar= Calendar.getInstance();
//		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
		return timestamp;
	}
	public static String getDate() {
		Date day=new Date();    
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
		return df.format(day);
	}
	
	/**
	 * 传入一个Timestamp startTime的时间参数和要增加的天数，返回增加后的天数
	 * @param startTime
	 * @param day
	 * @return
	 */
	public static Timestamp addTimeByDays(Timestamp startTime ,int day) {
	    Calendar endCal = Calendar.getInstance();
	    endCal.setTimeInMillis(startTime.getTime());
	    endCal.add(Calendar.DAY_OF_YEAR, day);
	    Timestamp time =new Timestamp(endCal.getTimeInMillis());
		return time;
	}
	/**
	* 字符串转换成日期
	* @param str
	* @return date
	*/
	public static Timestamp StrToDate(String str) {
//		str+=" 00:00:00";
		Timestamp timestamp = null;
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//yyyy-MM-dd HH:mm:ss
	    Date date = null;
	    try {
	        date = format.parse(str);
	        timestamp = new Timestamp(date.getTime());
	    } catch (ParseException e) {
	        e.printStackTrace();
	    }
	    return timestamp;
	}
	
	public static Timestamp StrToDateTime(String str) {
//		str+=" 00:00:00";
		Timestamp timestamp = null;
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//yyyy-MM-dd HH:mm:ss
	    Date date = null;
	    try {
	        date = format.parse(str);
	        timestamp = new Timestamp(date.getTime());
	    } catch (ParseException e) {
	        e.printStackTrace();
	    }
	    return timestamp;
	}
	
	
	
	/**
	 * 把时间的小时数加1
	 * 输入格式: 08:00:00  || 11:30:00
	 * @param str
	 * @return 	09:00:00  || 12:30:00
	 */
	public static String hoursAddOne(String str) {
		String hours = null;
		String ext = null;
		hours = String.valueOf((Integer.parseInt(str.substring(0, 2))+1));
		if(hours.length()!=2)
			hours = "0" + hours;
		ext = str.substring(2, str.length());
	    return hours+ext;
	}
	
	/**
	 * 把时间的小时数减 1
	 * 输入格式: 08:00:00  || 11:30:00
	 * @param str
	 * @return 	07:00:00  || 10:30:00
	 */
	public static String hoursSubOne(String str) {
		String hours = null;
		String ext = null;
		hours = String.valueOf((Integer.parseInt(str.substring(0, 2))-1));
		if(hours.length()!=2)
			hours = "0" + hours;
		ext = str.substring(2, str.length());
	    return hours+ext;
	}
	
	
	public static int betweenDays(Timestamp startCal , Timestamp endCal ) {
		return Math.abs((int)(startCal.getTime() - endCal.getTime())/(1000*60*60*24));
	}
	
	 public static void main(String[] args) {
		 Date day=new Date();    

		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		 
		 String string  = df.format(day);
		 System.out.println(string);  
		 
		 System.out.println(getDateTime());  
		 
		 System.out.println(StrToDate("2019-06-03"));  
		 
		 System.out.println(hoursSubOne("11:30:00")); 
	    
	}
		
}
